package com.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.model.UserInfo;

import io.swagger.annotations.Api;

@Api(tags = { "Restful API demo" })
@RestController
public class RestfulController {

	@PostMapping(value = "/v1/user")
	public void addUser(@RequestBody UserInfo userInfo) {

	}

	@DeleteMapping(value = "/v1/user/{id}")
	public void deleteUser(@PathVariable String id) {

	}

	@PutMapping(value = "/v1/user")
	public void updateUser(@RequestBody UserInfo userInfo) {

	}

	@GetMapping(value = "/v1/user")
	public List<UserInfo> getUsers() {
		List<UserInfo> users = new ArrayList<UserInfo>();
		users.add(new UserInfo("zhangsan", "123456"));
		users.add(new UserInfo("lisw", "123456"));
		users.add(new UserInfo("wangwu", "123456"));
		return users;
	}

	@GetMapping(value = "/")
	public String index() {
		return "hello zhangsike 第五次构建";
	}

	@GetMapping(value = "/test")
	public String indexs() {
		return "hello zhangsike test，第三次构建";
	}

	@PostMapping(value = "/upload")
	public String upload(@RequestParam MultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		String path = "/data/springboot-demo/" + fileName;
		File tmpFile = FileUtils.getFile(path);
		if (!tmpFile.getParentFile().isDirectory())
			tmpFile.getParentFile().mkdirs();
		file.transferTo(tmpFile);
		return "上传成功";
	}

}
